package top.tagao.yygh.user.controller.api;

import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.security.krb5.internal.AuthContext;
import top.tagao.yygh.common.result.Result;
import top.tagao.yygh.common.utils.AuthContextHolder;
import top.tagao.yygh.model.user.Patient;
import top.tagao.yygh.user.service.PatientService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/12 17:11
 */

@Api(tags = "就诊人管理")
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    @ApiOperation(value = "就诊人列表")
    @GetMapping("auth/findAll")
    public Result authFindAll(HttpServletRequest request) {
        Long userID = AuthContextHolder.getUserID(request);
        List<Patient> list = patientService.findAllUserId(userID);
        return  Result.ok(list);
    }
    //@ApiOperation(value = "就诊人查看详情")

    @ApiOperation(value = "就诊人添加")
    @PostMapping("auth/save")
    public Result authSave(@RequestBody Patient patient, HttpServletRequest request){
        //获取当前用户的Id
        Long userID = AuthContextHolder.getUserID(request);
        patient.setUserId(userID);
        patientService.save(patient);
        return Result.ok();
    }

    @ApiOperation(value = "就诊人修改")
    @PostMapping("auth/update")
    public  Result authUpdate(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }


    @ApiOperation(value = "就诊人查看详情")
    @GetMapping("auth/get/{id}")
    public Result getPatientById(@PathVariable Long id) {
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);

    }


    @ApiOperation(value = "就诊人删除")
    @DeleteMapping("auth/remove/{id}")
    public Result removePatientById(@PathVariable Long id) {
        patientService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "获取就诊人")
    @GetMapping("inner/get/{id}")
    public Patient getPatient(
            @ApiParam(name = "id", value = "就诊人id", required = true)
            @PathVariable("id") Long id){

        return  patientService.getPatientById(id);
    }

}
